package com.naihe.anderson.sms.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.naihe.anderson.sms.dbhelper.DbReadWriteLock;
import com.naihe.anderson.sms.dbhelper.RecordMoneyDBHelper;
import com.naihe.anderson.sms.entity.CapitalInfo;
import com.naihe.anderson.sms.entity.RecordMoney;
import com.naihe.anderson.sms.util.FileUtil;

public class RecordMoneyDao {

	private RecordMoneyDBHelper dbHelper;
	private SQLiteDatabase db;
	private static DbReadWriteLock dbReadWriteLock = new DbReadWriteLock();

	public RecordMoneyDao(Context context) {
		dbHelper = new RecordMoneyDBHelper(context);
		db = dbHelper.getWritableDatabase();
	}

	public void close() {
		if (dbHelper != null) {
			dbHelper.close();
		}
	}	
	
	/**111
	 * 
	 * @param list
	 */
	public void deleteRecordMoney(List<RecordMoney> list){
		try {
			dbReadWriteLock.writeLock();
			db.beginTransaction();
			 for(RecordMoney recordMoney : list){
				 db.delete(RecordMoneyDBHelper.RECORDMONEY_TABLE_NAME, RecordMoney.RMID + " = ? ", new String[]{recordMoney.rmid + ""});
			 }
			db.setTransactionSuccessful();
		} catch (Exception e) {
			FileUtil.addLog("RecordMoneyDao::deleteRecordMoney", RecordMoneyDao.class, "RecordMoneyDao", e);
		} finally {
			db.endTransaction();
			dbReadWriteLock.writeUnlock();
		}
	}
	
	/**
	 * 
	 * @param list
	 */
	public void deleteRecordMoneyEntity(RecordMoney recordMoney){
		try {
			dbReadWriteLock.writeLock();
			db.delete(RecordMoneyDBHelper.RECORDMONEY_TABLE_NAME, RecordMoney.RMID + " = ? ", new String[]{recordMoney.rmid + ""});
		} catch (Exception e) {
			FileUtil.addLog("RecordMoneyDao::deleteRecordMoneyEntity", RecordMoneyDao.class, "RecordMoneyDao", e);
		} finally {
			dbReadWriteLock.writeUnlock();
		}
	}
	
	
	
	/**
	 * 
	 * @param list
	 */
	public void insertOrUpdateRecordMoneyEntity(List<RecordMoney> list){
		try {
			 dbReadWriteLock.writeLock();
			 db.beginTransaction();
			 ContentValues values = new ContentValues();
			 for(RecordMoney recordMoney : list){
				 values.put(RecordMoney.RMID,recordMoney.rmid);
				 values.put(RecordMoney.MONEYNUM,recordMoney.moneyNum);
				 values.put(RecordMoney.PROJECT,recordMoney.project);
				 values.put(RecordMoney.PROJECTPIC,recordMoney.projectPic);
				 values.put(RecordMoney.CREATETIME,recordMoney.createTime);
				 values.put(RecordMoney.OBJECTMEMBER,recordMoney.objectMember);
				 values.put(RecordMoney.REMARKS,recordMoney.remarks);
				 values.put(RecordMoney.ACTIONTYPE,recordMoney.actionType);
				 values.put(RecordMoney.LOATION,recordMoney.loation);
				 
				 db.replace(RecordMoneyDBHelper.RECORDMONEY_TABLE_NAME, null, values);
				 values.clear();
			 }
			 values = null;
			 db.setTransactionSuccessful();
		}catch (Exception e) {
			FileUtil.addLog("RecordMoneyDao::insertOrUpdateRecordMoneyEntity", RecordMoneyDao.class, "RecordMoneyDao", e);
		}finally{
			db.endTransaction();
			dbReadWriteLock.writeUnlock();
		}
	}
	
	/**
	 * 
	 */
	public void insertOrUpdateRecordMoneyEntity(RecordMoney recordMoney){
		try {
			 dbReadWriteLock.writeLock();
			 ContentValues values = new ContentValues();
			 values.put(RecordMoney.RMID,recordMoney.rmid);
			 values.put(RecordMoney.MONEYNUM,recordMoney.moneyNum);
			 values.put(RecordMoney.PROJECT,recordMoney.project);
			 values.put(RecordMoney.PROJECTPIC,recordMoney.projectPic);
			 values.put(RecordMoney.CREATETIME,recordMoney.createTime);
			 values.put(RecordMoney.OBJECTMEMBER,recordMoney.objectMember);
			 values.put(RecordMoney.REMARKS,recordMoney.remarks);
			 values.put(RecordMoney.ACTIONTYPE,recordMoney.actionType);
			 values.put(RecordMoney.LOATION,recordMoney.loation);
			 
			 db.replace(RecordMoneyDBHelper.RECORDMONEY_TABLE_NAME, null, values);
			 values = null;
		}catch (Exception e) {
			FileUtil.addLog("RecordMoneyDao::insertOrUpdateRecordMoneyEntity", RecordMoneyDao.class, "RecordMoneyDao", e);
		}finally{
			dbReadWriteLock.writeUnlock();
		}
	}
	
	public List<RecordMoney> queryAllRecordMoneyList(long beginTime, long endTime) {
		 List<RecordMoney> list = new ArrayList<RecordMoney>();
		 
		 Cursor smscur = null;
		 try {
			 dbReadWriteLock.readLock();
			 smscur = db.query(RecordMoneyDBHelper.RECORDMONEY_TABLE_NAME, RecordMoneyDBHelper.recordmoney_projection, 
					 RecordMoney.CREATETIME + ">=" + beginTime + " and " + RecordMoney.CREATETIME + "<" + endTime, null, null, null, RecordMoney.CREATETIME + " desc");
			 if (smscur != null && smscur.moveToFirst()) {
				for (int i = 0; i < smscur.getCount(); i++) {
					RecordMoney entity = new RecordMoney();
					entity.rmid = smscur.getLong(smscur.getColumnIndex(RecordMoney.RMID));
					entity.actionType = smscur.getShort(smscur.getColumnIndex(RecordMoney.ACTIONTYPE));
					entity.createTime = smscur.getLong(smscur.getColumnIndex(RecordMoney.CREATETIME));
					entity.loation = smscur.getString(smscur.getColumnIndex(RecordMoney.LOATION));
					entity.moneyNum = smscur.getFloat(smscur.getColumnIndex(RecordMoney.MONEYNUM));
					entity.objectMember = smscur.getString(smscur.getColumnIndex(RecordMoney.OBJECTMEMBER));
					entity.project = smscur.getString(smscur.getColumnIndex(RecordMoney.PROJECT));
					entity.projectPic = smscur.getString(smscur.getColumnIndex(RecordMoney.PROJECTPIC));
					entity.remarks = smscur.getString(smscur.getColumnIndex(RecordMoney.REMARKS));
					
					list.add(entity);
					smscur.moveToNext();
				}
			 }
		 } catch (Exception e) {
			 FileUtil.addLog("RecordMoneyDao::queryAllRecordMoneyList", RecordMoneyDao.class, "RecordMoneyDao", e);
		 } finally{
			 if (smscur != null) { smscur.close(); }
			 dbReadWriteLock.readUnlock();
		 }
		 
		 return list;
	 }
	
	public List<RecordMoney> queryRecordMoneyList(String objectmember) {
		 List<RecordMoney> list = new ArrayList<RecordMoney>();
		 
		 Cursor smscur = null;
		 try {
			 dbReadWriteLock.readLock();
			 smscur = db.query(RecordMoneyDBHelper.RECORDMONEY_TABLE_NAME, RecordMoneyDBHelper.recordmoney_projection, 
					 RecordMoney.OBJECTMEMBER + "=" + objectmember, null, null, null, RecordMoney.CREATETIME + " desc");
			 if (smscur != null && smscur.moveToFirst()) {
				for (int i = 0; i < smscur.getCount(); i++) {
					RecordMoney entity = new RecordMoney();
					entity.rmid = smscur.getLong(smscur.getColumnIndex(RecordMoney.RMID));
					entity.actionType = smscur.getShort(smscur.getColumnIndex(RecordMoney.ACTIONTYPE));
					entity.createTime = smscur.getLong(smscur.getColumnIndex(RecordMoney.CREATETIME));
					entity.loation = smscur.getString(smscur.getColumnIndex(RecordMoney.LOATION));
					entity.moneyNum = smscur.getFloat(smscur.getColumnIndex(RecordMoney.MONEYNUM));
					entity.objectMember = smscur.getString(smscur.getColumnIndex(RecordMoney.OBJECTMEMBER));
					entity.project = smscur.getString(smscur.getColumnIndex(RecordMoney.PROJECT));
					entity.projectPic = smscur.getString(smscur.getColumnIndex(RecordMoney.PROJECTPIC));
					entity.remarks = smscur.getString(smscur.getColumnIndex(RecordMoney.REMARKS));
					
					list.add(entity);
					smscur.moveToNext();
				}
			 }
		 } catch (Exception e) {
			 FileUtil.addLog("RecordMoneyDao::queryAllRecordMoneyList", RecordMoneyDao.class, "RecordMoneyDao", e);
		 } finally{
			 if (smscur != null) { smscur.close(); }
			 dbReadWriteLock.readUnlock();
		 }
		 
		 return list;
	 }
	
	//====================================
	
	/**
	 * 
	 * @param list
	 */
	public void deleteCapitalEntity(CapitalInfo capitalInfo){
		try {
			dbReadWriteLock.writeLock();
			db.delete(RecordMoneyDBHelper.CAPITALINFO_TABLE_NAME, CapitalInfo.CID + " = ? ", new String[]{capitalInfo.cid + ""});
		} catch (Exception e) {
			FileUtil.addLog("RecordMoneyDao::deleteCapitalEntity", RecordMoneyDao.class, "RecordMoneyDao", e);
		} finally {
			dbReadWriteLock.writeUnlock();
		}
	}
	
	public List<CapitalInfo> queryAllCapitalInfoList(long beginTime, long endTime) {
		 List<CapitalInfo> list = new ArrayList<CapitalInfo>();
		 
		 Cursor smscur = null;
		 try {
			 dbReadWriteLock.readLock();
			 smscur = db.query(RecordMoneyDBHelper.CAPITALINFO_TABLE_NAME, RecordMoneyDBHelper.capitalInfo_projection, 
					 CapitalInfo.CREATE_TIME + ">=" + beginTime + " and " + CapitalInfo.CREATE_TIME + "<" + endTime, null, null, null, CapitalInfo.CREATE_TIME + " desc");
			 if (smscur != null && smscur.moveToFirst()) {
				for (int i = 0; i < smscur.getCount(); i++) {
					CapitalInfo capitalInfo = new CapitalInfo();
					capitalInfo.cid = smscur.getLong(smscur.getColumnIndex(CapitalInfo.CID));
					capitalInfo.createTime = smscur.getLong(smscur.getColumnIndex(CapitalInfo.CREATE_TIME));
					capitalInfo.expenditureKeys = smscur.getString(smscur.getColumnIndex(CapitalInfo.EXPENDITURE_KEYS));
					capitalInfo.incomeIeys = smscur.getString(smscur.getColumnIndex(CapitalInfo.INCOME_KEYS));
					capitalInfo.number = smscur.getString(smscur.getColumnIndex(CapitalInfo.NUMBER));
					list.add(capitalInfo);
					smscur.moveToNext();
				}
			 }
		 } catch (Exception e) {
			 FileUtil.addLog("RecordMoneyDao::queryAllCapitalInfoList", RecordMoneyDao.class, "RecordMoneyDao", e);
		 } finally{
			 if (smscur != null) { smscur.close(); }
			 dbReadWriteLock.readUnlock();
		 }
		 
		 return list;
	 }
	/**
	 * 
	 */
	public void insertOrUpdateCapitalInfoEntity(CapitalInfo capitalInfo){
		try {
			 dbReadWriteLock.writeLock();
			 ContentValues values = new ContentValues();
			 values.put(CapitalInfo.CID, capitalInfo.cid);
			 values.put(CapitalInfo.CREATE_TIME, capitalInfo.createTime);
			 values.put(CapitalInfo.EXPENDITURE_KEYS, capitalInfo.expenditureKeys);
			 values.put(CapitalInfo.INCOME_KEYS, capitalInfo.incomeIeys);
			 values.put(CapitalInfo.NUMBER, capitalInfo.number);
			 
			 db.replace(RecordMoneyDBHelper.CAPITALINFO_TABLE_NAME, null, values);
			 values = null;
		}catch (Exception e) {
			FileUtil.addLog("RecordMoneyDao::insertOrUpdateCapitalInfoEntity", RecordMoneyDao.class, "RecordMoneyDao", e);
		}finally{
			dbReadWriteLock.writeUnlock();
		}
	}
}
